//
//  NMNotificationRequestLocation.m
//  NotificationMe
//
//  Created by max on 2018/12/16.
//  Copyright © 2018年 dayang. All rights reserved.
//

#import "NMNotificationRequestLocation.h"
#import <MapKit/MapKit.h>

@implementation NMNotificationRequestLocation
+ (UNNotificationRequest *)requestWithContent:(NSDictionary *)content {
    UNMutableNotificationContent *nContent = [[UNMutableNotificationContent alloc] init];
    nContent.title = content[@"title"];
    nContent.body = content[@"body"];
    
    if ([content[@"sound"] boolValue]) {
        nContent.sound = [UNNotificationSound defaultSound];
    }
    
    CGFloat latitude = [content[@"latitude"] floatValue];
    CGFloat longitude = [content[@"longitude"] floatValue];
    
    CGFloat radius = [content[@"radius"] floatValue];
    
    BOOL notifyOnEntry = [content[@"notifyOnEntry"] boolValue];
    BOOL notifyOnExit = [content[@"notifyOnExit"] boolValue];
    BOOL repeat = [content[@"repeat"] boolValue];
    
    CLLocationCoordinate2D location = CLLocationCoordinate2DMake(latitude,longitude);
    CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:location radius:radius identifier:nContent.title];
    region.notifyOnEntry = notifyOnEntry;
    region.notifyOnExit = notifyOnExit;
    UNLocationNotificationTrigger *trigger = [UNLocationNotificationTrigger triggerWithRegion:region repeats:repeat];
    return [UNNotificationRequest requestWithIdentifier:[self identifier]
                                                content:nContent trigger:trigger];
}
@end
